package cn.com.aop;

import cn.com.config.AppDataSourceContext;
import cn.com.config.UserDataSourceContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AppDsAop {

    @Pointcut("@annotation(cn.com.aop.annotation.AppDs)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Long userId = UserDataSourceContext.getUserId();
        //使用app数据源
        AppDataSourceContext.setApp(Boolean.TRUE);
        try {
            return joinPoint.proceed();
        } finally {
            //归还userId
            UserDataSourceContext.setUserId(userId);
            AppDataSourceContext.remove();
        }
    }
}
